﻿<%@ Page Title="" Language="C#" MasterPageFile="~/uc/MemberIndex.Master" validateRequest="false" AutoEventWireup="true" CodeBehind="product_add.aspx.cs" Inherits="ncp.Web.member.product_add" %>
<asp:Content ID="Content1" ContentPlaceHolderID="header" runat="server">
<title>产品展示-中国农业信息化服务商</title>
<script src="/Plugin/ueditor/editor_config.js" type="text/javascript"></script>

    <link href="/Plugin/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
    <script src="/Plugin/ueditor/editor_all.js" type="text/javascript"></script>
    <link href="/Plugin/ueditor/themes/default/ueditor.css" rel="stylesheet" type="text/css" />
    <script src="/Plugin/My97DatePicker/WdatePicker.js" type="text/javascript"></script>

    <script src="/Plugin/uploadify/jquery.uploadify-3.1.min.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="bh-con-right-h-yy"><a href="#">店铺管理</a> > <a href="/member/shop_product.html">产品展示</a> > 添加产品</div>
<div id="item-publish">
    <div class="bd-main">
        <div class="title">产品信息</div>
        <div class="form mod-content">
            <table>
                <tr>
                    <td class="mod-label"> <em>*</em><label for="proName">产品标题:</label></td>
                    <td><input type="text" class="bh-input-long" maxlength="50" name="proName" id="proName"/><span class="msg"></span></td>
                </tr>
                <tr>
                    <td class="mod-label"><em>*</em><label for="proNum">产品数量:</label></td>
                    <td><input type="text" class="bh-input-small" maxlength="5" id="proNum"/>&nbsp;
                    <select class="bh-select" id="unit">
                        <option value="件">件</option>
                        <option value="千克">千克</option>
                        <option value="吨">吨</option>
                        <option value="亩">亩</option>
                        <option value="米">米</option>
                        <option value="个">个</option>
                        <option value="其他">其他</option>
                    </select>
                    <input type="text" id="otherunit" style="display: none;" maxlength="5" class="bh-input-small"/>
                    <span class="msg"></span></td>
                </tr>
                <tr>
                    <td class="mod-label"><em>*</em><label for="proNum">产品分类:</label></td>
                    <td>
                        <select id="proClass1" class="bh-select">
                            <option value="">--请选择--</option>
                        </select>
                        <select id="proClass2" class="bh-select">
                            <option value="">--请选择--</option>
                        </select>
                        <select id="proInfo" class="bh-select">
                            <option value="">--请选择--</option>
                        </select>
                    <span class="msg"></span>
                    
                    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><em>*</em><label for="proPlace">产品产地:</label></td>
                    <td>
                    <select id="povince" class="bh-select">
                            <option value="">--请选择--</option>
                        </select>
                        <select id="city" class="bh-select">
                            <option value="">--请选择--</option>
                        </select>
                        <select id="area" class="bh-select">
                            <option value="">--请选择--</option>
                        </select><span class="msg"></span>
                        <div style="line-height: 40px;height:40px;">详细地址：<input type="text" class="bh-input-long" maxlength="50" name="detailPlace" id="detailPlace"/></div>
                        
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"><em>*</em><label for="endDate">到期时间:</label></td>
                    <td><input type="text" class="bh-input-def" maxlength="10" onclick="WdatePicker()" id="endDate"/><span class="msg"></span></td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><em>*</em><label for="proPlace" style="margin-top: 0px;">产品图片:</label></td>
                    <td style="vertical-align:top;">
                        <div id="uploadify1">
                        </div>
                        <span class="msg"></span>
                        <ul id="imageList">
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><label for="proPlace" style="margin-top: 0px;">产品描述:</label></td>
                    <td id="proRemark" style="height: 250px;">
                        <script type="text/plain" id="prointro"></script>
                    </td>
                </tr>
                <tr>
            <td colspan="2" style="text-align: center;border-bottom: none;">
                <input type="button" class="bh-lr-btn" onclick="onSaveProduct()" value="保 存"/>
                <input type="button" class="bh-lr-btn" onclick="history.go(-1)" value="返 回"/>
            </td>
        </tr>
        <tr style="border: none;">
            <td colspan="2" style="border: none;">
                注：<br/>
                1、带*号的项为必填！<br/>
                2、产品产地至少填写一项！
            </td>
        </tr>
            </table>
        </div>
    </div>
    <script type="text/javascript">
        $(function() {
        $("#unit").change(function() {
                if ($(this).val() == "其他") {
                    $("#otherunit").show();
                } else {
                $("#otherunit").hide();
                }
            });
        });
        //加载编辑器
        var editor = new baidu.editor.ui.Editor({
            toolbars: simpleToolbar,
            minFrameHeight: 250,
            maximumWords: 1000,
            elementPathEnabled: false
        });
        editor.render("prointro");
    	
        function appendList(fileName, fileaddr) {
            var list = "<li><span title=\"删除\" onclick=\"onDelete(this)\" class=\"imgdelete\"></span><img onclick=\"setDefault(this)\" src=\"" + fileaddr + "\"/><div class=\"imgName\">" + fileName + "</div></li>";
            $("#imageList").append(list);
            if ($(".defTitle").length == 0) {
                $("#imageList li:eq(0)").append("<div class=\"defTitle\">封面图片</div>");
            }
        }
        $(function() {
            getProductCategory("proClass1");
            getArea("povince", 0, "");
            var proid = '<%=Request.QueryString["proid"] %>';
            if (proid != '') {
                loadProductInfo();
            }
            $("#proClass1").change(function() {
                var class1 = $(this).val();
                getProductClass(class1, "proClass2");
                getProductInfo(class1);
            });
            $("#proClass2").change(function() {
                getProductInfo($("#proClass2").val());
            });
            $("#povince").change(function() {
                var city = $(this).val();
                getArea("city", 1, city);
                getArea("area", 2, $("#city").val());
            });
            $("#city").change(function() {
                getArea("area", 2, $("#city").val());
            });

            //获取cookie用户名
            $.ajax({
                url: "/handler/CookieHandler.ashx",
                type: "POST",
                dataType: "text",
                success: function(data) {
                    if (data == "") {
                        alert("获取用户信息失败，请退出重新登录！");
                        return;
                    }
                    $("#uploadify1").uploadify({
                        'swf': '/Plugin/uploadify/uploadify.swf',
                        'auto': true,
                        'fileTypeExts': '*.gif; *.jpg; *.png',
                        'uploader': '/handler/UploadHandler.ashx',
                        'multi': true,
                        'formData': { 'folder': '/uploadFiles', 'uname': data },
                        'buttonText': '本地图片',
                        "fileSizeLimit":imgSizeLimit,
                        'onUploadSuccess': function(file, d, response) {
                            if (response) {
                                if (data != '0') {
                                    appendList(file.name, d);
                                } else {
                                    alert("图片上传失败！");
                                }
                            }
                        }
                    });
                }
            });
        });
        function setDefault(obj) {
            $("div.defTitle").remove();
            $(obj).parent().append("<div class=\"defTitle\">封面图片</div>");
        }
        //删除图片
        function onDelete(obj) {
            //删除服务器图片
            var src = $(obj).parents("li").children("img").attr("src");
            if (src == undefined || src == "") {
                $(obj).parents("li").remove();
                return;
            }
            $.ajax({
                url: "/handler/ImageDelete.ashx",
                type: "POST",
                dataType: "text",
                data: "imgsrc=" + src,
                success: function(data) {
                    if (data == "1" || data == "0") {
                        $(obj).parents("li").remove();
                        if ($(".defTitle").length == 0) {
                            $("#imageList li:eq(0)").append("<div class=\"defTitle\">封面图片</div>");
                        }
                    }
                }
            });
        }

        function onSaveProduct() {
            $("span.msg").text("");
            if ($.trim($("#proName").val())=="") {
                $("#proName").next("span.msg").text("产品标题不能为空！");
                $("#proName").focus();
                return;
            }
            if ($.trim($("#proNum").val())=="") {
                $("#otherunit").next("span.msg").text("产品数量不能为空！");
                $("#proNum").focus();
                return;
            }
            if (isNaN($("#proNum").val())) {
                $("#otherunit").next("span.msg").text("请正确填写产品数量！");
                $("#proNum").focus();
                return;
            }
            var unit = $("#unit").val();
            if (unit == "其他") {
                $("#otherunit").next("span.msg").text("单位不能为空！");
                $("#otherunit").focus();
                return;
            }
            if ($("#proInfo").val()=="") {
                $("#proInfo").next("span.msg").text("请选择产品分类！");
                $("#proInfo").focus();
                return;
            }
            if ($.trim($("#area").val())=="") {
                $("#area").next("span.msg").text("请选中产品产地！");
                $("#area").focus();
                return;
            }
            if ($.trim($("#endDate").val())=="") {
                $("#endDate").next("span.msg").text("到期时间不能为空！");
                $("#endDate").focus();
                return;
            }
            if ($("#imageList li").length==0) {
                $("#imageList").prev("span.msg").text("请添加图片！");
                return;
            }
            var ele = new Object();
            ele.proid = '<%=Request.QueryString["proid"] %>';
            ele.productname = $("#proName").val();
            ele.pronum = $("#proNum").val();
            ele.area = $("#area").val();
            ele.detail = $("#detailPlace").val();
            ele.end = $("#endDate").val();
            ele.g_id = $("#proInfo").val();
            ele.cover = $("#imageList li .defTitle").parent().children("img").attr("src");
            ele.pic = '';
            
            ele.unit = unit == "其他" ? $("#otherunit").val() : unit;
            $("#imageList li ").each(function() {
                ele.pic += "|" + $(this).children("img").attr("src");
            });
            ele.content = editor.getContent();
            var response = ncp.Web.member.product_add.OnSaveProduct($.objectToJson(ele));
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                alert(jsonOBJ.msg);
                if (jsonOBJ.isreload) {
                    if (ele.proid == "") {
                        dataEmpty();
                    }
                    else {
                        location.href = "/member/shop_product.html";
                    }
                }
            }
        }
        function dataEmpty() {
            $("#proName,#proClass1,#proClass2,#proInfo,#proNum,#endDate,#povince,#city,#area,#detailPlace").val("");
            $("#imageList").html("");
            editor.setContent("");
        }
        
        //获取产品分类
        function getProductInfo(gcId) {
            var response = ncp.Web.member.product_add.GetProInfo(gcId);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $("#proInfo").html(jsonOBJ.msg);
                } else {
                    alert(jsonOBJ.msg);
                }
            }
        }
        //商品大类
        function getProductClass(pId,dom) {
            var response = ncp.Web.member.product_add.GetProClass(pId);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $("#" + dom).html(jsonOBJ.msg);
                }else {
                    alert(jsonOBJ.msg);
                }
            }
        }
        function getProductCategory(dom) {
            var response = ncp.Web.member.product_add.GetProCategory();
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $("#" + dom).html(jsonOBJ.msg);
                } else {
                    alert(jsonOBJ.msg);
                }
            }
        }
        //加载信息
        function loadProductInfo() {
            var proId = '<%=Request.QueryString["proid"] %>';
            var response = ncp.Web.member.product_add.GetProductInfo(proId);
            if (response.value != null) {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $("#proName").val(jsonOBJ.Product_Title);
                    $("#proNum").val(jsonOBJ.Product_Amount);
                    $("#povince").val(jsonOBJ.province);
                    getArea("city", 1, jsonOBJ.province);
                    $("#city").val(jsonOBJ.city);
                    getArea("area", 2, jsonOBJ.city);
                    $("#area").val(jsonOBJ.area);
                    $("#detailPlace").val(jsonOBJ.detail);
                    $("#endDate").val(jsonOBJ.Product_EndTime);
                    $("#proClass1").val(jsonOBJ.classId);
                    getProductClass(jsonOBJ.classId.toString(), "proClass2");
                    $("#proClass2").val(jsonOBJ.classInfo);
                    if ($("#unit").text().indexOf(jsonOBJ.unit) == -1) {
                        $("#unit").val("其他");
                        $("#otherunit").show();
                        $("#otherunit").val(jsonOBJ.unit);
                    } else {
                        $("#unit").val(jsonOBJ.unit);
                    }
                    getProductInfo(jsonOBJ.classInfo.toString());
                    $("#proInfo").val(jsonOBJ.proInfo);
                    appendList("", jsonOBJ.Product_Picture);
                    var img = jsonOBJ.pic.split("|");
                    for (i = 0; i < img.length; i++) {
                        if ($("#imageList li img").attr("src") != img[i])
                            appendList("", img[i]);
                    }
                    var remark = "<script type=\"text/plain\" id=\"prointro\">" + jsonOBJ.Product_Remark + "<\/script>";
                    $("#proRemark").html(remark);
                } else {
                    alert(jsonOBJ.msg);
                    history.go(-1);
                }
            }
        }
        //获取地区
        function getArea(dom, type, pId) {
            var response = ncp.Web.member.product_add.GetArea(type, pId);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                if (jsonOBJ.isreload) {
                    $("#" + dom).html(jsonOBJ.msg);
                } else {
                    alert(jsonOBJ.msg);
                }
            }
        }

    </script>
    
</div>

</asp:Content>
